package com.cloudcc.mobile.view.base;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.widget.Toast;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.ui.dialog.DialogHelper;
import com.cloudcc.cloudframe.ui.dialog.WaitDialog;
import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.mobile.AppManager;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dialog.CallLogLoadingDialog;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.net.KeepSessionClient;
import com.cloudcc.mobile.weight.LoadingDialog;
import com.easemob.easeui.controller.EaseUI;

public class CommonActivity extends FragmentActivity implements
		OnCancelListener {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	/**
	 * 在后台
	 */

	public volatile static int forwordFlog = 0;

	public static final String TAG = Class.class.getSimpleName();

	protected CallLogLoadingDialog mWaiting;

	protected Context mContext;
	/**
	 * 正在加载
	 */
	private WaitDialog _waitDialog;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		mContext = this;
		AppManager.getInstance().push(this);
		// 事件总线
		if (hasEventLife()) {
			EventEngine.register(this);
		}
	}

	/**
	 * 显示 等待 的 dialog
	 * 
	 * @param message
	 *            提示语
	 * @return 返回 LoadingDialog
	 */
	protected CallLogLoadingDialog showWainting(String message) {
		mWaiting = new CallLogLoadingDialog(this, R.style.DialogLoadingTheme);
		mWaiting.show();
		mWaiting.settext(message);
		return mWaiting;
	}

	/**
	 * 显示 等待 的 dialog
	 * 
	 * @return 返回 LoadingDialog
	 */
	protected CallLogLoadingDialog showWainting() {
		mWaiting = new CallLogLoadingDialog(this,R.style.DialogLoadingTheme);
		return mWaiting;
	}

	/**
	 * 取消 显示等待的dialog
	 */
	protected void dismissWainting() {
		if (mWaiting != null) {
			mWaiting.dismiss();
			mWaiting = null;
		}
	}

	@Override
	protected void onStart() {
		LogUtils.d("forground", "onStart::" + this.toString());
		forwordFlog++;
		checkRuningState();
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
		try {
			EaseUI.getInstance().getNotifier().reset();
		} catch (Exception e) {
		}

	}

	@Override
	protected void onStop() {
		LogUtils.d("forground", "onStop::" + this.toString());
		forwordFlog--;
		checkRuningState();
		super.onStop();
	}

	private void checkRuningState() {
		LogUtils.d("forground", "当前标志位::" + forwordFlog);
		if (forwordFlog > 0) {// 前台
			KeepSessionClient.getInstance().refreshServerBinding();
		}
	}

	public WaitDialog showWaitDialog(String message) {

		if (_waitDialog == null) {
			_waitDialog = DialogHelper.getWaitDialog(this, message);

		}
		if (_waitDialog != null) {
			_waitDialog.setMessage(message);
			_waitDialog.setOnCancelListener(this);
			_waitDialog.show();
		}
		return _waitDialog;
	}

	public void hideWaitDialog() {
		if (_waitDialog != null) {
			try {
				_waitDialog.dismiss();
				_waitDialog = null;
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public WaitDialog showWaitDialog() {

if ("en".equals(mEns)) {
	return showWaitDialog("Loading...");
	}else{
		return showWaitDialog("正在加载...");
}
		
	}

	public WaitDialog showWaitDialog(int resid) {
		return showWaitDialog(getString(resid));
	}

	/**
	 * 取消dialog的时候
	 * */
	@Override
	public void onCancel(DialogInterface dialog) {
		onCancelWaitDialog(dialog);
	}

	/**
	 * 正在加载取消的时候回调
	 * 
	 * @param dialog
	 */
	public void onCancelWaitDialog(DialogInterface dialog) {
	}

	@Override
	protected void onDestroy() {
		if (hasEventLife()) {
			EventEngine.uregister(this);
		}
		super.onDestroy();
		AppManager.getInstance().pop(this);

	}

	/**
	 * show toast
	 * 
	 * @param msg
	 */
	protected void showToast(String msg) {
		if (null != msg && !TextUtils.isEmpty(msg)) {
			Toast.makeText(mContext, msg, 0).show();
		}
	}

	/**
	 * 是否 注册到总线
	 * 
	 * @return
	 */
	protected boolean hasEventLife() {
		return false;
	}

}
